package dk.modified.radioonlineapp;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteDBHelper extends SQLiteOpenHelper {

	public static final String TABLE_USERDATA = "userData";
	public static final String TABLE_RADIOS = "radios";
	public static final String TABLE_MOST = "most";
	public static final String TABLE_FAV = "fav";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_RADIOURL = "radioURL";
	public static final String COLUMN_SLUG = "slug";
	public static final String COLUMN_RATING = "rating";
	public static final String COLUMN_VERSION = "version";
	public static final String COLUMN_COUNT = "count";
	public static final String COLUMN_RADIOACTIVE = "radioActive";
	public static final String COLUMN_COUNTRY = "country";

	private static final String DATABASE_NAME = "radios.db";
	private static final int DATABASE_VERSION = 1;
	
	private static final String CREATE_TABLE_USERDATA =
			" create table " + 
			TABLE_USERDATA +
			" (" + 
			COLUMN_ID + 
			" integer primary key autoincrement, " +
			COLUMN_RADIOACTIVE + 
			" integer, " + 
			COLUMN_VERSION + 
			" integer, " + 
			COLUMN_COUNTRY + 
			" text);";

	
	private static final String CREATE_TABLE_RADIOS =
			" create table " + 
			TABLE_RADIOS +
			" (" +
			COLUMN_ID +
			" integer, " +
			COLUMN_NAME +
			" text, " +
			COLUMN_RADIOURL +
			" text, " +
			COLUMN_SLUG +
			" text, " +
			COLUMN_COUNTRY +
			" text, " +
			COLUMN_RATING +
			" integer primary key autoincrement);";
	
	private static final String CREATE_TABLE_MOST =
			" create table " +
			TABLE_MOST +
			" (" + 
			COLUMN_ID +
			" integer primary key, " +
			COLUMN_COUNT +
			" integer, foreign key (" + 
			COLUMN_ID + 
			") references " + 
			TABLE_RADIOS + 
			"(" + 
			COLUMN_ID + 
			"));";
	
	private static final String CREATE_TABLE_FAV =
			" create table " + 
			TABLE_FAV +
			" (" +
			COLUMN_ID +
			" integer primary key, " +
			COLUMN_NAME +
			" text, " +
			COLUMN_RADIOURL +
			" text, " +
			COLUMN_SLUG +
			" text, " + 
			COLUMN_COUNTRY +
			" text);";
	
	public SQLiteDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_USERDATA);
		db.execSQL(CREATE_TABLE_RADIOS);
		db.execSQL(CREATE_TABLE_MOST);
		db.execSQL(CREATE_TABLE_FAV);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(SQLiteDBHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_RADIOS);
		onCreate(db);
	}
}